//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<XWindowManager.cpp>
///	@path	~/src/editor/wm/
///	@date	2008/05/16
///	@desc	.

#include "editor/editor.h"

#include "lib/system/sysinfo.h"

#include "gui/image/CColorBitmap.h"
#include "gui/image/CSpriteBitmap.h"

#include "editor/wm/XWindowManager.h"
#include "editor/xedit/XEditDesktop.h"
#include "editor/editor_engine.h"

namespace xeres {

	// wm
	XWindowManager& editor::wm( void )
	{
		static XWindowManager s_XWindowManager;
		return s_XWindowManager;
	}

	// XWindowManager
	XWindowManager::XWindowManager( void )
		: m_id( 0 )
		, m_mainView( NULL )
		, m_mainFrame( NULL )
	{
	}
	// ~XWindowManager
	XWindowManager::~XWindowManager( void )
	{

	}
	// AttachMainView
	void XWindowManager::AttachMainView( HWND mainFrame , XEditorMain * mainView )
	{
		XS_ASSERT( m_mainView == NULL );
		m_mainView = mainView;
		m_mainFrame = mainFrame;
	}
	// OpenDialog
	int XWindowManager::OpenDialog( Ref<XForm> form )
	{
		// Get screen size
		Pair<int> size = form->GetSize();
		XFormDialog dialog( Ref<IGuiForm>( form ) , form->GetSize() , &XWindowManager::TickByDialog );
		m_dialogs.push_back( &dialog );
		int result = (int)dialog.DoModal( m_mainFrame );
		XS_ASSERT( m_dialogs.back() == &dialog );
		m_dialogs.pop_back();
		return result;
	}
	// CloseDialog
	void XWindowManager::CloseDialog( int result )
	{
		XS_ASSERT( m_dialogs.size() > 0 );
		XFormDialog * dlg = m_dialogs.back();
		dlg->EndDialog( result );
	}
	// TickByDialog
	void XWindowManager::TickByDialog( void )
	{
		// tick call inside dialog.
		XEditor::instance().engine().UpdateFrame();
	}
	
} // namespace xeres
